﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Landscape.Diagnostics;

namespace Landscape.Web.Controls {
    public partial class DiagnosticsControl : System.Web.UI.UserControl {
        protected global::System.Web.UI.WebControls.Label lblTime;
        protected global::System.Web.UI.WebControls.Label lblCount;
        protected global::System.Web.UI.WebControls.Table tblTasks;

        protected override void OnInit(EventArgs e) {
            string tpl = Landscape.Resources.GetEmbeddedResource("Landscape.Web.Controls.DiagnosticsControl.ascx");
            Control uc = Page.ParseControl(tpl);
            Controls.Add(uc);

            lblTime = (Label)FindControl("lblTime");
            lblCount = (Label)FindControl("lblCount");
            tblTasks = (Table)FindControl("tblTasks");

            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e) {
            try {
                RenderSession sess = (RenderSession)Session["rendererlastsession"];
                lblCount.Text = sess.Tasks.Count.ToString();
                lblTime.Text = sess.Duration.ToString();
                TableRow head = new TableRow() {
                    Cells = { 
                    new TableHeaderCell() { Text = "Order", HorizontalAlign = HorizontalAlign.Center },
                    new TableHeaderCell() { Text = "Path", HorizontalAlign = HorizontalAlign.Left },
                    new TableHeaderCell() { Text = "Duration", HorizontalAlign = HorizontalAlign.Left },
                },
                    TableSection = TableRowSection.TableHeader
                };
                tblTasks.Rows.Add(head);
                if (sess.Tasks.Count > 0) {
                    int i = 0;
                    foreach (RenderTask task in sess.Tasks) {
                        TableRow row = new TableRow() {
                            TableSection = TableRowSection.TableBody,
                            CssClass = i % 2 == 0? "alt": ""
                        };
                        row.Cells.Add(new TableCell() {
                            Text = (task.Order + 1).ToString(),
                            HorizontalAlign = HorizontalAlign.Center
                        });
                        row.Cells.Add(new TableCell() {
                            Text = task.TemplatePath,
                            HorizontalAlign = HorizontalAlign.Left
                        });
                        row.Cells.Add(new TableCell() {
                            Text = task.Duration.ToString(),
                            HorizontalAlign = HorizontalAlign.Center
                        });
                        tblTasks.Rows.Add(row);
                        i++;
                    }
                }
            } catch  {
                this.Visible = false;
            }
        }
    }
}